package com.kyzh.core.adapters.providers

import android.animation.ObjectAnimator
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.core.beans.GiftList
import com.kyzh.core.R
import com.kyzh.core.adapters.BiaoqianAdapter
import com.gushenge.core.toast

class GiftProvider() :BaseNodeProvider(){
    override val itemViewType: Int
        get() = 0

    override val layoutId: Int
        get() = R.layout.gift_item_1
    override fun convert(helper: BaseViewHolder, item: BaseNode) {
        val data = item as GiftList
        val bq = helper.getView<RecyclerView>(R.id.bq)
        val desc = helper.getView<TextView>(R.id.desc)
        val start = helper.getView<ImageView>(R.id.start)
        Glide.with(context).load(data.icon).into(helper.getView(R.id.image))
        if (data.biaoqian.isNotEmpty()) {
            bq.visibility = View.VISIBLE
            desc.visibility = View.GONE

            bq.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
            bq.adapter = BiaoqianAdapter(item.biaoqian)
        } else {
            bq.visibility = View.GONE
            desc.visibility = View.VISIBLE
        }
        helper.setText(R.id.type, String.format(context.getString(R.string.giftNum2),data.lb_num))
                .setText(R.id.name, data.name)

        if (data.isExpanded) {
            ObjectAnimator.ofFloat(start, "rotation", -90f).setDuration(500).start()
        } else {
            ObjectAnimator.ofFloat(start, "rotation", 0f).setDuration(500).start()
        }

    }

    override fun onClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {

        val start = helper.getView<ImageView>(R.id.start)
        if ((data as GiftList).list.isEmpty()) {
            context.getString(R.string.thisGameHaveNoGift).toast()
        } else {
            if (data.isExpanded) {
                getAdapter()?.collapse(position, false)
                ObjectAnimator.ofFloat(start, "rotation", 0f).setDuration(500).start()
            } else {
                getAdapter()?.expand(position, false)
                ObjectAnimator.ofFloat(start, "rotation", 90f).setDuration(500).start()
            }
        }
    }
}